#!/usr/bin/python3
# 获取内盘交易所的结算参数，存到CSV文件

import requests
import csv
import json
import re
import io
import datetime
import sys
import time

def json2csv(json_datas):
   csv_datas = []
   flag = True
   for json_data in json_datas:
       if flag:
           csv_datas.append(list(json_data.keys()))
           flag = False
       csv_datas.append(list(json_data.values()))
   return csv_datas;

def json2csv2(json_datas,keys):
   csv_datas = []
   csv_datas.append(keys)
   for json_data in json_datas:
       values = [json_data[key] for key in keys]
       csv_datas.append(values)
   return csv_datas;

def str2csv(text,keys):
   csv_datas = []
   reader = csv.reader(io.StringIO(text))
   is_head = True;
   cols = []
   for line_list in reader:
       if len(line_list) > 2:
           if is_head:
               cols = [line_list.index(key) for key in keys]
               is_head = False
           new_line_list = [line_list[col] for col in cols]
           csv_datas.append(new_line_list)
   return csv_datas

def writecsv(path, datas):
    file = open(path + '.csv', 'w', newline='')
    writer = csv.writer(file, delimiter=',')
    for data in datas:
        writer.writerow(data)


def before_date(date=datetime.datetime.now()):
    return date+datetime.timedelta(days=-1)


def date_str(date):
    return date.strftime('%Y%m%d')


def down_cffex_clear_params(path):
    date = before_date()
    for i in range(0,30):
        datestr = date_str(date)
        url = 'http://www.cffex.com.cn/sj/jscs/%s/%s/%s_1.csv' % (datestr[0:6], datestr[6:8], datestr)
        html = requests.get(url, allow_redirects=False)
        html.encoding='gbk'
        if html.status_code == 200:
            print('获取中金所[%s]的结算参数参数成功' % datestr)
            text = str2csv(html.text,["期货合约","合约多头保证金标准"])
            writecsv(path+'/CFFEX', text)
            break
        date = before_date(date)


def down_shfe_clear_params(path):
    date = before_date()
    for i in range(0,30):
        datestr = date_str(date)
        url = 'http://www.shfe.com.cn/data/dailydata/js/js%s.dat' % datestr
        html = requests.get(url, allow_redirects=True)
        if html.status_code == 200 and 'o_cursor' in html.json() and len(html.json()['o_cursor']) > 0:
            print('获取上交所[%s]的结算参数参数成功' % datestr)
            # csv_datas = json2csv2((html.json())['o_cursor'],['PRODUCTID','PRODUCTNAME','INSTRUMENTID','TRADEFEERATIO','SPECLONGMARGINRATIO'])
            csv_datas = json2csv2((html.json())['o_cursor'],['INSTRUMENTID','SPECLONGMARGINRATIO'])
            writecsv(path+'/SHFE', csv_datas)
            break
        date = before_date(date)


def down_ine_clear_params(path):
    date = before_date()
    headers= {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"
            }
    for i in range(0,30):
        datestr = date_str(date)
        url = 'http://www.ine.com.cn/data/dailydata/js/js%s.dat' % datestr
        html = requests.get(url, headers=headers, allow_redirects=True)
        if html.status_code == 200 and 'o_cursor' in html.json() and len(html.json()['o_cursor']) > 0:
            print('获取原油交易所[%s]的结算参数参数成功' % datestr)
            csv_datas = json2csv2((html.json())['o_cursor'],['INSTRUMENTID','SPECLONGMARGINRATIO'])
            writecsv(path+'/INE', csv_datas)
            break
        date = before_date(date)


def down_dce_clear_params(path):
    date = before_date()
    for i in range(0,30):
        datestr = date_str(date)
        url = 'http://www.dce.com.cn/publicweb/businessguidelines/exportFutAndOptSettle.html'
        data = {}
        data['variety'] = 'all'
        data['trade_type'] = '0'
        data['exportFlag'] = 'txt'
        data['year'] = str(date.year)
        data['month'] = str(date.month-1)
        data['day'] = str(date.day)
        html = requests.post(url, data=data)
        if html.status_code == 200 and html.text.count('\r') > 5:
            print('获取大商所[%s]的结算参数参数成功' % datestr)
            text = html.text.replace(',', '')
            text = re.sub('([^\t])\t',r'\1,',text)
            text = text.replace('\t', '')
            text = str2csv(text,["合约代码","投机买保证金率"])
            writecsv(path+'/DCE', text)
            break
        date = before_date(date)


def down_zce_clear_params(path):
    date = before_date()
    for i in range(0,30):
        datestr = date_str(date)
        url = 'http://www.czce.com.cn/cn/DFSStaticFiles/Future/%s/%s/FutureDataClearParams.txt' % (datestr[0:4], datestr)
        html = requests.get(url, allow_redirects=False)
        # html.encoding='gbk'
        if html.status_code == 200:
            print('获取郑商所[%s]的结算参数参数成功' % datestr)
            text = html.text.replace(',', '').replace('|', ',')
            text = str2csv(text,["合约代码","交易保证金率(%)"])
            writecsv(path+'/ZCE', text)
            break
        date = before_date(date)

def down_gfex_clear_params(path):
    date = before_date()
    for i in range(0,30):
        datestr = date_str(date)
        url = 'http://www.gfex.com.cn/u/interfacesWebTiFutAndOptSettle/loadList'
        data = 'trade_date=%s'%datestr
        header = {'Content-Type': 'application/x-www-form-urlencoded',
              'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54'}
        html = requests.post(url, data=data, headers=header)
        if html.status_code == 200 and 'data' in html.json() and len(html.json()['data']) > 0:
            print('获取广期所[%s]的结算参数参数成功' % datestr)
            csv_datas = json2csv2((html.json())['data'],['contractId','specBuyRate'])
            writecsv(path+'/GFEX', csv_datas)
            break
        date = before_date(date)
        break

if __name__ == '__main__':
    path = sys.argv[1]
    down_cffex_clear_params(path)
    down_shfe_clear_params(path)
    down_dce_clear_params(path)
    down_zce_clear_params(path)
    down_gfex_clear_params(path)
    down_ine_clear_params(path)
